package com.ljx.video.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2025-01-18  00:31
 */
public class FileUtil {

    public static String getFileExtension(String fileName) {
        int lastIndexOfDot = fileName.lastIndexOf(".");
        if (lastIndexOfDot == -1) {
            // 如果文件名没有扩展名，返回空字符串
            return "";
        }
        return fileName.substring(lastIndexOfDot + 1);
    }

    public static void removeFile(String filePath) {
        Path path = Paths.get(filePath);
        try {
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void renameFile(String oldFilePath, String newFilePath) {
        Path oldPath = Paths.get(oldFilePath);
        Path newPath = Paths.get(newFilePath);
        try {
            Files.move(oldPath, newPath);  // 移动并重命名文件
            System.out.println("File renamed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Failed to rename file.");
        }
    }

}
